package com.lobo.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Blog;
import com.lobo.blog.entity.Type;
import com.lobo.blog.entity.User;
import com.lobo.blog.service.AdminBlogService;
import com.lobo.blog.service.AdminTagService;
import com.lobo.blog.service.AdminTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/10/4
 * @description:
 */
@Controller
@RequestMapping("/admin")
public class AdminBlogController {

    @Resource
    AdminBlogService adminBlogService;
    @Resource
    AdminTypeService adminTypeService;
    @Resource
    AdminTagService adminTagService;

    @RequestMapping(value = "/blogs", method = RequestMethod.GET)
    public String toAdminBlogs(Model model) {
        PageInfo<Blog> blogs = adminBlogService.getBlogs(1);
        List<Type> types = adminTypeService.getAllType();
        model.addAttribute("types", types);
        model.addAttribute("page", blogs);
        System.out.println(blogs);
        return "admin/blogs";
    }

    /**
     * 跳转到添加页面
     *
     * @param model 返回前端tags和type
     * @return
     */
    @RequestMapping(value = "/addBlog", method = RequestMethod.GET)
    public String toAdminAddBlog(Model model) {
        model.addAttribute("types", adminTypeService.getAllType());
        model.addAttribute("tags", adminTagService.getAllTags());
        model.addAttribute("blog", new Blog());
        return "admin/addBlog";
    }

    @PostMapping("/blogs/addBlog")
    public String addBlog(Blog blog, RedirectAttributes attributes, HttpSession httpSession) {
        blog.setUser((User) httpSession.getAttribute("user"));
        System.err.println(blog.getFlag());
        int result = adminBlogService.saveBlog(blog);
        if (result != 0) {
            attributes.addFlashAttribute("msg", "操作成功");
        } else {
            attributes.addFlashAttribute("msg", "操作失败");
        }
        return "redirect:/admin/blogs";
    }


    @RequestMapping("/updateBlog/{blogId}")
    public String toUpdateBlogPage(
            @PathVariable("blogId") Integer blogId,
            Model model) {
        Blog blog = adminBlogService.getBlogById(blogId);
        blog.setTagsList(adminTagService.getTagByIds(blog.getTags()));
        model.addAttribute("types", adminTypeService.getAllType());
        model.addAttribute("tags", adminTagService.getAllTags());
        model.addAttribute("blog", blog);
        return "admin/addBlog";
    }

    @RequestMapping(value = "/blogs/search", method = RequestMethod.POST)
    public String getAdminBlogList(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                   @RequestParam(value = "title", required = false) String title,
                                   @RequestParam(value = "typeId", required = false) Integer type,
                                   @RequestParam(value = "recommend", required = false) Boolean recommend,
                                   Model model) {
        if (pageNum == null) {
            pageNum = 1;
        }
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setTypeId(type);
        blog.setRecommend(recommend);
        PageInfo<Blog> blogs = adminBlogService.getBlogsByCondition(pageNum, blog);
        model.addAttribute("page", blogs);
        return "admin/blogs :: blogList";
    }

    @RequestMapping("/blogs/delete/{blogId}")
    public String deleteBlogById(@PathVariable("blogId") Integer blogId,
                                 RedirectAttributes attributes) {
        int result = adminBlogService.deleteBlogById(blogId);
        if (result != 0) {
            attributes.addFlashAttribute("msg", "操作成功");
        } else {
            attributes.addFlashAttribute("msg", "操作失败");
        }
        return "redirect:/admin/blogs";
    }
}
